import Mock from 'mockjs'

const questionList = Mock.mock({
  'list|20-50': [{
    'id|+1': 1,
    title: '@ctitle(10, 25)',
    'type|1': ['1', '2', '3', '4'], // 1: 单选；2: 多选；3: 填空；4: 判断
    questionImage: '',
    questionVideo: '',
    questionOptionList: () => {
        // Simple mock for options
        return [
            { id: 101, questionId: '@id', optionContent: '选项A' },
            { id: 102, questionId: '@id', optionContent: '选项B' }
        ]
    },
    answerList: () => {
        // Simple mock for answers
        return [{ id: 101, questionId: '@id', optionContent: '选项A' }]
    },
    analysisList: [],
    'createBy|+1': 100,
    createName: '@cname',
    createTime: '@datetime',
    'open|1': [true, false],
    'status|1': [0, 1, -1] // 0: 待审核；1: 已发布；-1: 已驳回
  }]
}).list

// 题目列表
Mock.mock(/\/api\/web\/question\/pageQueryQuestionList/, 'post', (options) => {
  const { pageNum = 1, pageSize = 10, title, status } = JSON.parse(options.body)
  
  let filteredList = questionList;
  if (title) {
    filteredList = filteredList.filter(q => q.title.includes(title));
  }
  if (status !== undefined && status !== '') {
    filteredList = filteredList.filter(q => q.status === status);
  }

  const list = filteredList.slice((pageNum - 1) * pageSize, pageNum * pageSize)
  return {
    code: '200',
    resData: {
      total: filteredList.length,
      list,
      pageNum,
      pageSize
    },
    msg: '查询成功'
  }
})

// 添加题目
Mock.mock(/\/api\/web\/question\/addQuestion/, 'post', () => {
  return {
    code: '200',
    resData: null,
    msg: '添加成功'
  }
})

// 更新题目 (新增的 mock, 视图需要)
Mock.mock(/\/api\/web\/question\/updateQuestion/, 'post', () => {
  return {
    code: '200',
    resData: null,
    msg: '更新成功'
  }
})

// 删除题目
Mock.mock(/\/api\/web\/question\/deleteQuestion/, 'post', () => {
  return {
    code: '200',
    resData: null,
    msg: '删除成功'
  }
})

// 审核题目
Mock.mock(/\/api\/web\/question\/auditQuestion/, 'post', (options) => {
  const { questionId, status } = JSON.parse(options.body)
  const question = questionList.find(q => q.id === questionId)
  if (question) {
    question.status = status
  }
  return {
    code: '200',
    resData: null,
    msg: '审核成功'
  }
})